package com.fin.zw.aiqas.controller;

import com.fin.zw.aiqas.annotation.CurrentUser;
import com.fin.zw.aiqas.model.request.UserInfoRequest;
import com.fin.zw.aiqas.model.response.DeptAndUsersResponse;
import com.fin.zw.aiqas.model.response.UserInfoResponse;
import com.fin.zw.aiqas.model.session.SessionModel;
import com.fin.zw.aiqas.service.DepartmentService;
import com.fin.zw.aiqas.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(value = "用户相关管理",description = "用户相关管理")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private DepartmentService departmentService;

    @PostMapping("/add")
    @ApiOperation(value = "创建用户信息",notes = "创建用户信息")
    public ResponseEntity<Void> createUserInfo(@RequestBody UserInfoRequest request, @ApiIgnore @CurrentUser SessionModel sessionModel){
        userInfoService.createUserInfo(request,sessionModel);
        return ResponseEntity.ok().body(null);
    }

    @RequestMapping(value = "/update",method = {RequestMethod.PUT,RequestMethod.PATCH})
    @ApiOperation(value = "用户信息更新",notes = "用户信息更新")
    public ResponseEntity<Void> updateUserInfo(@RequestBody UserInfoRequest request, @ApiIgnore @CurrentUser SessionModel sessionModel){
        userInfoService.updateUserInfo(request,sessionModel);
        return ResponseEntity.ok().body(null);
    }


    @GetMapping("/fetchPage")
    @ApiOperation(value = "查询所有的用户信息",notes = "查询所有的用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<UserInfoResponse>> getUserInfoAll(Pageable pageable,
                                                                 UserInfoRequest request,
                                                                 @ApiIgnore @CurrentUser SessionModel sessionModel) {
        Page<UserInfoResponse> page = userInfoService.getUserInfoAll(request, pageable, sessionModel);
        return ResponseEntity.ok().body(page);
    }


    @GetMapping("/fetchListByRoleId")
    @ApiOperation(value = "根据角色查询用户", notes = "根据角色查询用户")
    public ResponseEntity<Page<UserInfoResponse>> getUserInfoAll(Pageable pageable,
                                                                 @RequestParam("roleIds") Long roleId,
                                                                 @RequestParam(value = "accountName", required = false) String accountName,
                                                                 @RequestParam(value = "userName", required = false) String userName) {
        Page<UserInfoResponse> page = userInfoService.getListByRoleId(pageable, roleId, accountName, userName);
        return ResponseEntity.ok().body(page);
    }

    @GetMapping("/fetchAllDeptAndUserInfos")
    @ApiOperation(value = "查询所有部门下的的用户信息", notes = "查询所有部门下的的用户信息")
    public ResponseEntity<List<DeptAndUsersResponse>> fetchAllDeptAndUserInfos() {
        List<DeptAndUsersResponse> deptAndUsersResponseList = userInfoService.fetchAllDeptAndUserInfos();
        return ResponseEntity.ok().body(deptAndUsersResponseList);
    }

    @GetMapping("/fetchDeptAndUsersByRole")
    @ApiOperation(value = "查询每个部门拥有复核权限的所有人", notes = "查询每个部门拥有复核权限的所有人")
    public ResponseEntity<List<DeptAndUsersResponse>> fetchDeptAndUsersByRole() {
        List<DeptAndUsersResponse> deptAndUsersResponseList = userInfoService.fetchDeptAndUsersByRole();
        return ResponseEntity.ok().body(deptAndUsersResponseList);
    }
}
